//
//  FmNetworking.m
//  FantasyMonster
//
//  Created by 冯志强 on 2024/1/29.
//

#import "FmNetworking.h"
#import <AFNetworking/AFNetworking.h>
//#import "LoginVC.h"
@interface FmNetworking()
@property (nonatomic, strong) AFHTTPSessionManager *manager;

@end

@implementation FmNetworking

static FmNetworking *_instance = nil;
+ (instancetype)shareInstance
{
    static dispatch_once_t onceToken ;
    dispatch_once(&onceToken, ^{
        _instance = [[super allocWithZone:NULL] init];
        _instance.manager = [AFHTTPSessionManager manager];
        _instance.manager.requestSerializer = [AFJSONRequestSerializer serializer];
        _instance.manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"application/json", @"text/json", @"text/javascript",nil];
        _instance.manager.requestSerializer.timeoutInterval = 30.f;
    }) ;
    return _instance;
}

-(void)POST:(NSString *)url params:( NSDictionary *__nullable)params
    success:(FMResponseSuccess)success fail:(FMesponseFail)fail{
    
    NSLog(@"post========%@",url);
    
    [_instance.manager.requestSerializer setValue:[NSString stringWithFormat:@"Bearer %@",TOKEN] forHTTPHeaderField:@"Authorization"];

    [_instance.manager POST:url parameters:params headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        FHideLoding
        NSInteger code = [[responseObject objectForKey:@"code"] integerValue];
        if (code == 0 || code == 200) {
            success(task,responseObject,code);
        }
        else{
            FhudShowStr(responseObject[@"msg"]);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

    }];
}

-(void)GET:(NSString *)url params:(NSDictionary *__nullable)params
 success:(FMResponseSuccess)success fail:(FMesponseFail)fail{
    
    NSLog(@"post========%@======%@",url,TOKEN);
    
    [_instance.manager.requestSerializer setValue:[NSString stringWithFormat:@"Bearer %@",TOKEN] forHTTPHeaderField:@"Authorization"];
    
    [_instance.manager GET:url parameters:params headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSInteger code = [[responseObject objectForKey:@"code"] integerValue];
        if (code == 0 || code == 200) {
            success(task,responseObject,code);
        }
        else{
            FhudShowStr(responseObject[@"msg"]);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}

- (void)updateIdImage:(UIImage*)image url:(NSString *)url name:(NSString *)name
              success:(FMResponseSuccess)success fail:(FMesponseFail)fail
{
    [_instance.manager POST:url parameters:@{} headers:nil constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        NSData *imageData = UIImageJPEGRepresentation(image, 0.5);
        [formData appendPartWithFileData :imageData name:name fileName:[NSString stringWithFormat:@"%@.png",[self setObjcName]] mimeType:@"multipart/form-data"];
    } progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSInteger code = [[responseObject objectForKey:@"code"] integerValue];
        if (code == 0 || code == 200) {
            success(task,responseObject,code);
        }
        else{
            FHideLoding
            FhudShowStr(responseObject[@"msg"]);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
       
    }];
}





- (NSString*)setObjcName
{
    NSString * string = [NSString stringWithFormat:@"%ld_image_%@",(long)UserId,[self roundStr]];
    return string;
}
- (NSString*)roundStr
{
    NSDate *currentDate = [NSDate date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"hh_mm_ss"];
    NSString *dateString = [dateFormatter stringFromDate:currentDate];
    return dateString;
}
@end
